/*
* © 2010 Autodata Solutions Company. All Rights Reserved
* This source code is the confidential and proprietary information of Autodata Solutions Company.
* The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
* without the prior written consent of Autodata Solutions Company.
*
* File: net/autodata/compare/widgets/EditorialToolbarView.js
* Purpose: Provides custom implimintation of the compare's editorial toolbar view.
*
* Namespace: net.autodata.compare
* Requires:ExtJS,
*          ASC library - net/autodata/ascjs-ext-debug.js,
*          AbstractView - net/autodata/widgets/AbstractView.js,
*/

Ext.ns('net.autodata.compare.EditorialToolbarView');
(function() {

    var toggleGroup = Ext.id(undefined, 'competitor-vehicle-selector-btns');
    /**
    * Custom view of CompetitorToolbar
    * @class AdvantagesToolbarView
    * @extends net.autodata.AbstractView
    * @namespace net.autodata.compare
    */
    var View = Ext.extend(net.autodata.AbstractView, {
        
    	buttonWidth: 147,
    	lastButtonWidth: 148,
        lastButtonIndex: 2,
        
    	baseButtonWidth: 334,
        
    	
        buttonIdPrefix: 'btnSelectVehicle_',

        constructor: function() {
            this.addEvents({ 'vehicleselected': true });
            View.superclass.ctor.apply(this, arguments);
        },

        /**
        * @method init
        */
        init: function() {
            View.superclass.init.apply(this, arguments);

            var tb = this.component;
            tb.relayEvents(this, ['vehicleselected']);
            var store = tb.getStore();
            store.on('load', this.render, this);
        },

        /**
        * @method render
        */
        render: function() {
            var tb = this.component;
            tb.removeAll(true);

            var store = tb.getStore();

            var selectVehicleText = ASC.message('net.autodata.compare.toolbar.label.select-vehicle');
            
            var lbi = this.lastButtonIndex || store.getCount();
            var bw = this.buttonWidth;
            var lbw = this.lastButtonWidth || bw;
            var bbw = this.baseButtonWidth;
            
            
            store.each(function(vehicle, index) {
                var width = tb.items != null && tb.items.getCount() > 0 ? bw : bbw;

                if (Ext.isEmpty(vehicle.get('acode'))) {
                    tb.add({ xtype: 'box', width: width });
                }
                else {
                    var acode = vehicle.get('acode');
                    if (!this.selectedAcode) {
                        this.selectedAcode = acode;
                    }

                    tb.addButton({
                        xtype: 'a-btn-toolbar',
                        itemId: this.buttonIdPrefix + acode,
                        text: selectVehicleText,
                        hide: false,
                        toggleGroup: toggleGroup,
                        width: index < lbi ? width : lbw,
                        listeners: {
                            'click': this.selectVehicle.createDelegate(this, [acode], 1)
                        }
                    });
                }
            }, this, false);

            if (tb.boxReady) {
                tb.doLayout(false, true);
            }
        },

        /**
        * Triggers the vehicle selected event.
        * @method onButtonClick
        * @param {Ext.Button} button Ext Button object that triggered the click event.
        * @param {string} acode The selected vehicle's acode.
        * @private
        */
        selectVehicle: function(button, acode) {
            if (this.selectedAcode === acode) {
                button.toggle(true, false);
                return;
            }
            this.selectedAcode = acode;
            this.fireEvent('vehicleselected', acode);
        },

        /**
        * @method onDestroy
        * @private
        */
        onDestroy: function() {
            this.component.removeAll();
        }
    });

    net.autodata.compare.EditorialToolbarView = View;
})();
